/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "stdint.h"
#include "stdio.h"
#include "stm32f1xx_ll_rcc.h"
#include "main.h"
#include "usart.h"

#include "DevicesList.h"
#include "DevicesQueue.h"
#include "DevicesDelay.h"
#include "DevicesLed.h"
#include "DevicesKey.h"
#include "DevicesTime.h"
#include "DevicesTimer.h"
#include "DevicesSoftTimer.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
    KeyTypeDef typeKeyData = {0};
    int64_t lUNIXTimeStamp = 0;

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* 更新 SystemCoreClock 的值，延时函数会用到它 */
  SystemCoreClockUpdate();

  /* USER CODE BEGIN SysInit */

  vDelayInit();

  /* 在使用之前就需要先进行初始化 */
  enumQueueInit();

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_UART5_Init();
  /* USER CODE BEGIN 2 */
  printf("\n\n\rSystemCoreClock: %d Hz.\r\n", (int)SystemCoreClock);

  if(LL_RCC_IsActiveFlag_LPWRRST() != 0)
      printf("低功耗复位.\r\n");
  if(LL_RCC_IsActiveFlag_WWDGRST() != 0)
      printf("窗口看门狗复位.\r\n");
  if(LL_RCC_IsActiveFlag_IWDGRST() != 0)
      printf("独立看门狗复位.\r\n");
  if(LL_RCC_IsActiveFlag_SFTRST() != 0)
      printf("软件复位.\r\n");
  if(LL_RCC_IsActiveFlag_PORRST() != 0)
      printf("上电/掉电复位.\r\n");
  if(LL_RCC_IsActiveFlag_PINRST() != 0)
      printf("NRST引脚复位.\r\n");
  /* 清除复位标置 */
  __HAL_RCC_CLEAR_RESET_FLAGS();

  vLedInit();

  vKeyInit();

  /* 系统时基 */
  cTimer6Init();

  /* 周期调用状态机 */
  cTimer5Init();

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* 读取按键输入消息 */
    while(enumQueuePopDatas(&g_TypeQueueKeyInput, (uint8_t *)&typeKeyData, sizeof(typeKeyData)) == queueNormal)
    {
      if((typeKeyData.state == keyShort) && (typeKeyData.valuePress == KEY_RESET))
        printf("if((typeKeyData.state == keyShort) && (typeKeyData.valuePress == KEY_RESET))\r\n");

      else if((typeKeyData.state == keyShort) && (typeKeyData.valuePress == KEY_START))
        printf("else if((typeKeyData.state == keyShort) && (typeKeyData.valuePress == KEY_START))\r\n");

      else if((typeKeyData.state == keyShort) && (typeKeyData.valuePress == (KEY_RESET | KEY_START)))
        printf("else if((typeKeyData.state == keyShort) && (typeKeyData.valuePress == (KEY_RESET | KEY_START)))\r\n");


      else if((typeKeyData.state == (keyShort | keyCut)) && (typeKeyData.valuePress == KEY_RESET))
        printf("else if((typeKeyData.state == (keyShort | keyCut)) && (typeKeyData.valuePress == KEY_RESET))\r\n");

      else if((typeKeyData.state == (keyShort | keyCut)) && (typeKeyData.valuePress == KEY_START))
        printf("else if((typeKeyData.state == (keyShort | keyCut)) && (typeKeyData.valuePress == KEY_START))\r\n");

      else if((typeKeyData.state == (keyShort | keyCut)) && (typeKeyData.valuePress == (KEY_RESET | KEY_START)))
        printf("else if((typeKeyData.state == (keyShort | keyCut)) && (typeKeyData.valuePress == (KEY_RESET | KEY_START)))\r\n");


      else if((typeKeyData.state == keyLong) && (typeKeyData.valuePress == KEY_RESET))
        printf("if((typeKeyData.state == keyLong) && (typeKeyData.valuePress == KEY_RESET))\r\n");

      else if((typeKeyData.state == keyLong) && (typeKeyData.valuePress == KEY_START))
        printf("else if((typeKeyData.state == keyLong) && (typeKeyData.valuePress == KEY_START))\r\n");

      else if((typeKeyData.state == keyLong) && (typeKeyData.valuePress == (KEY_RESET | KEY_START)))
        printf("else if((typeKeyData.state == keyLong) && (typeKeyData.valuePress == (KEY_RESET | KEY_START)))\r\n");


      else if((typeKeyData.state == (keyLong | keyCut)) && (typeKeyData.valuePress == KEY_RESET))
        printf("else if((typeKeyData.state == (keyLong | keyCut)) && (typeKeyData.valuePress == KEY_RESET))\r\n");

      else if((typeKeyData.state == (keyLong | keyCut)) && (typeKeyData.valuePress == KEY_START))
        printf("else if((typeKeyData.state == (keyLong | keyCut)) && (typeKeyData.valuePress == KEY_START))\r\n");

      else if((typeKeyData.state == (keyLong | keyCut)) && (typeKeyData.valuePress == (KEY_RESET | KEY_START)))
        printf("else if((typeKeyData.state == (keyLong | keyCut)) && (typeKeyData.valuePress == (KEY_RESET | KEY_START)))\r\n");


      else if((typeKeyData.state == keyContinuous) && (typeKeyData.valuePress == KEY_RESET))
        printf("if((typeKeyData.state == keyContinuous) && (typeKeyData.valuePress == KEY_RESET))\r\n");

      else if((typeKeyData.state == keyContinuous) && (typeKeyData.valuePress == KEY_START))
        printf("else if((typeKeyData.state == keyContinuous) && (typeKeyData.valuePress == KEY_START))\r\n");

      else if((typeKeyData.state == keyContinuous) && (typeKeyData.valuePress == (KEY_RESET | KEY_START)))
        printf("else if((typeKeyData.state == keyContinuous) && (typeKeyData.valuePress == (KEY_RESET | KEY_START)))\r\n");


      else if((typeKeyData.state == (keyContinuous | keyCut)) && (typeKeyData.valuePress == KEY_RESET))
        printf("else if((typeKeyData.state == (keyContinuous | keyCut)) && (typeKeyData.valuePress == KEY_RESET))\r\n");

      else if((typeKeyData.state == (keyContinuous | keyCut)) && (typeKeyData.valuePress == KEY_START))
        printf("else if((typeKeyData.state == (keyContinuous | keyCut)) && (typeKeyData.valuePress == KEY_START))\r\n");

      else if((typeKeyData.state == (keyContinuous | keyCut)) && (typeKeyData.valuePress == (KEY_RESET | KEY_START)))
        printf("else if((typeKeyData.state == (keyContinuous | keyCut)) && (typeKeyData.valuePress == (KEY_RESET | KEY_START)))\r\n");


      lUNIXTimeStamp = lTimeGetStamp() / 1000000;
      printf("UTC: %s %s\r\n", pcStampToDateStrings(lUNIXTimeStamp), pcStampToTimeStrings(lUNIXTimeStamp));
    }

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
int fputc(int ch, FILE *f)
{
  /* e.g. write a character to the USARTx and Loop until the end of transmission */
  while((UART5->SR & UART_FLAG_TXE) == RESET);

  /* Transmit Data */
  UART5->DR = ch;

  return ch;
}

/* USER CODE END 4 */

/**
  * @brief  Period elapsed callback in non blocking mode
  * @note   This function is called  when TIM7 interrupt took place, inside
  * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  * a global variable "uwTick" used as application time base.
  * @param  htim : TIM handle
  * @retval None
  */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  /* USER CODE BEGIN Callback 0 */

  /* USER CODE END Callback 0 */
  if (htim->Instance == TIM7) {
    HAL_IncTick();
  }
  /* USER CODE BEGIN Callback 1 */

  /* USER CODE END Callback 1 */
}

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

